#! /bin/bash
# Custom Bru LTO tape verify script

PATH=$PATH:/bru
export PATH

BLOCKSIZE_K=1024
LTO_GEN=4
JOB_START_SECS=="$(date +%s)"

echo ""
echo "****************** BRU LTO Verify Script ******************"
echo ""


fnOperationProgress()
{
	if [ $1 == "c" ]; then
		msg_str="Archived"
	elif [ $1 == "i" ]; then
		msg_str="Verified"
	fi
	replace="n"
	while read line
	do
		echo "$line" | grep "^VL:$1" > /dev/null
		if [ $? -eq 0 ]; then
			secs=$(echo -n "$(date +%s)")
			elapsed_secs=$(echo -n "$[$secs - $start_secs]")	
			size_k=$(echo $line | cut -f 2 -d "|")
			size_m=$(echo "scale=2; $size_k / 1024" | bc)		
			size_g=$(echo "scale=2; $size_m / 1024" | bc)
			pcent=$(echo "scale=1; $size_k * 100 / $backup_size_k" | bc)
			elapsed_time=$(date -ur $elapsed_secs +%H:%M:%S)
			mbps=$(echo "scale=1; $size_m / $[$elapsed_secs + 1]" | bc)
			if [ $replace == "n" ]; then
				echo ""
			fi
			if [ $replace == "y" ]; then
				
				tput dl	
				tput cuu1
			fi
			echo "$msg_str $size_g Gb (${pcent}%) in $elapsed_time at $mbps Mbps"
			replace="y"
		else
			echo "$line" | grep "^P:" > /dev/null
			if [ $? -ne 0 ]; then
				if [ $replace == "y" ]; then
					tput dl

					tput cuu1
				fi
				echo "$line"
				replace="n"
			fi
		fi
	done
}



# Verify tape
echo "Verifying tape"
echo ""

start_secs="$(date +%s)"

bru -ivvvvvvvvv -j -b ${BLOCKSIZE_K}K -f ntape$TAPE | fnOperationProgress "i"

stop_secs="$(date +%s)"
elapsed_secs=$(echo -n "$[$stop_secs - $start_secs]")	
verify_time=$(date -ur $elapsed_secs +%H:%M:%S)
echo ""
echo "Verification took $verify_time"
echo ""

# Rewind tape
tapectl -v -f ntape$TAPE rewind

# Eject tape
tapectl -v -f ntape$TAPE unload

echo "DONE!"
echo ""
echo "[Please label tape]"
echo ""
exit
